package org.bigdata.framework.admin.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bigdata.framework.core.model.Classes;
import org.bigdata.framework.core.model.CmsNavbar;
import org.bigdata.framework.core.model.CmsNewsModule;
import org.bigdata.framework.core.model.DataProperty;
import org.bigdata.framework.core.model.Dict;
import org.bigdata.framework.core.model.DictItem;
import org.bigdata.framework.core.protocol.ICmsService;
import org.bigdata.framework.core.protocol.IDynamicService;
import org.bigdata.framework.core.protocol.IPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xxl.conf.core.XxlConfClient;




/**
 * 
 * ClassName: CmsContrller <br/> 
 * Function: TODO ADD FUNCTION. <br/> 
 * Reason: TODO ADD REASON(可选). <br/> 
 * date: 2017年10月29日 上午10:08:46 <br/> 
 * 门户内容管理 Controller
 * @author xwh
 * @version
 */

@Controller
public class CmsContrller extends BaseContoroller {
	@Autowired 
	private ICmsService cmsService;
	
	@Autowired
	private IPublicService publicService;
	
	@Autowired
	private IDynamicService dynamicService;
	/**
	 * 
	 * cmsNavbarList: 展示导航条管理页面<br/> 
	 * 
	 * @author xwh
	 * @return 
	 *
	 */
	@RequestMapping("cms-navbar-list.html")
	public String cmsNavbarList() {
		return "/modules/cms/cms-navbar-list";
	}
	
	/**
	 * 
	 * cmsNavbarAddUpload:展示添加编辑导航条页面 <br/> 
	 * 
	 * @author xwh
	 * @return 
	 *
	 */
	@RequestMapping("cms-navbar-addUpload.html")
	public String cmsNavbarAddUpload() {
		return "/modules/cms/cms-navbar-addUpload";
	}
	
	/**
	 * 
	 * cmsNavbarUser: 跳转所有用户<br/> 
	 * 
	 * @author xwh
	 * @return 
	 *
	 */
	@RequestMapping("cms-navbar-user.html")
	public String cmsNavbarUser() {
		return "/modules/cms/cms-navbar-user";
	}
	
	/**
	 * 
	 * cmsNavbarCategoryZtree:跳转新闻栏目 <br/> 
	 * 
	 * @author xwh
	 * @return 
	 *
	 */
	@RequestMapping("cms-navbar-category-ztree.html")
	public String cmsNavbarCategoryZtree() {
		return "/modules/cms/cms-navbar-category-ztree";
	}
	
	/**
	 * 
	 * 
	 * cmsNewsModuleList:跳转内容块管理页面 <br/> 
	 * 
	 * @author xwh
	 * @return 
	 *
	 */
	@RequestMapping("cms-news-module-list.html")
	public String cmsNewsModuleList() {
		return "/modules/cms/cms-news-module-list";
	}
	
	/**
	 * 
	 * cmsNewsModuleAddUpload: 跳转内容块增加修改页面<br/> 
	 * 
	 * @author xwh
	 * @return 
	 *
	 */
	@RequestMapping("cms-news-module-addUpload.html")
	public String cmsNewsModuleAddUpload() {
		return "/modules/cms/cms-news-module-addUpload";
	}
	
	/**
	 * 
	 * cmsNavbarMineZtree:跳转父节点树 <br/> 
	 * 
	 * @author xwh
	 * @return 
	 *
	 */
	@RequestMapping("cms-navbar-mine-ztree.html")
	public String cmsNavbarMineZtree() {
		return "/modules/cms/cms-navbar-mine-ztree";
	}
	
	/**
	 * 展示栏目管理
	 * @author liuyan 
	 * @return
	 */
	
	@RequestMapping("cms-category-list.html")
	public String cmsCategoryList(){
		return "/modules/cms/cms-category-list";
	}
	/**栏目新建加载
	 * @author liuyan 
	 * @return
	 */
	@RequestMapping("cms-category-addUpload.html")
	public String cmsCategoryAdd(){
		return "/modules/cms/cms-category-addUpload";
	}
	
	/**
	 * 
	 * cmsNewsCategoryAddZtree:跳转添加树 <br/> 
	 * 
	 * @author xwh
	 * @return 
	 *
	 */
	@RequestMapping("cms-news-category-add-ztree.html")
	public String cmsNewsCategoryAddZtree(){
		return "/modules/cms/cms-news-category-add-ztree";
	}
	
	
	/**
	 * 新闻栏目
	 * @author liuyan
	 * @return
	 */
	@RequestMapping("cms-news-list.html")
	public ModelAndView cmsNewsList(HttpServletRequest request,HttpServletResponse response){
		Integer departmentid =(Integer) request.getSession().getAttribute("userid");
        ModelAndView view = new ModelAndView();
		view.addObject("user", publicService.findUser(departmentid));
		view.setViewName("/modules/cms/cms-news-list");
		return view;
	}
	
	/**
	 * 添加新闻栏目
	 * @author liuyan 
	 * @return
	 */
	@RequestMapping("cms-news-addUpload.html")
	public String cmsNewsaddUpload(){
		
		return "/modules/cms/cms-news-addUpload";
	}
	/**
	 * 
	 * cmsNewsModuleType: 跳转块类型对应编辑页面<br/> 
	 * 
	 * @author xwh
	 * @return 
	 *
	 */
	@RequestMapping("cms-news-module-type.html")
	public String cmsNewsModuleType() {
		return "/modules/cms/cms-news-module-type";
	}
	
	/**
	 * 
	 * cmsNewsModuleType: 导航条选择栏目<br/> 
	 * 
	 * @author xwh
	 * @return 
	 * 
	 */
	@RequestMapping("cms-news-category-ztree.html")
	public String cmsNewsCategoryZtree() {
		return "/modules/cms/cms-news-category-ztree";
	}
	
	
	/**
	 * 
	 * toSelectClass: 查询所有知识库<br/> 
	 * 
	 * @author xwh
	 * @return 
	 * 
	 */
	@RequestMapping("selectClass")
	@ResponseBody
	public Map<String,Object> toSelectClass(){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			List<Classes> classes = publicService.getClasses();
			result.put("msg", "OK");
			result.put("data", classes);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}
	
	
	/**
	 * 
	 * toCmsNavbarCategoryZtree: 关联新闻栏目树查询<br/> 
	 * 
	 * @author xwh
	 * @return 
	 *
	 */
	@RequestMapping("cmsNavbarCategoryZtree")
	@ResponseBody
	public Map<String,Object> toCmsNavbarCategoryZtree(){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
			Map<String,Object> resultMap = null;
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("=,name_en", "news_category");
			List<Dict> dict = publicService.getDict(queryParams,0,2);
			HashMap<String, Object> queryParamd = new HashMap<String, Object>();
			queryParamd.put("=,dict_id", dict.get(0).getId());
			Integer count = publicService.getDictItemCount(queryParamd);
			List<DictItem> dictItem = publicService.getDictItem(queryParamd, 0, count);
			for (int i = 0; i < dictItem.size(); i++) {
				resultMap = new HashMap<String, Object>();
				resultMap.put("id", dictItem.get(i).getId());
				resultMap.put("name", dictItem.get(i).getNameZh());
				resultMap.put("value", dictItem.get(i).getValue());
				if(dictItem.get(i).getDictItemId() == null){
					resultMap.put("pId", 0);
				}else{
					resultMap.put("pId", dictItem.get(i).getDictItemId());
				}
				resultList.add(resultMap);
			}
			result.put("msg", "OK");
			result.put("data", resultList);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * toCmsNavbarmineZtree: 父节点树查询<br/> 
	 * 
	 * @author xwh
	 * @return 
	 *
	 */
	@RequestMapping("cmsNavbarmineZtree")
	@ResponseBody
	public Map<String,Object> toCmsNavbarmineZtree(){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
			Map<String,Object> resultMap = null;
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("=,status", 0);
			Integer count = cmsService.getCmsNavbarCount(queryParams);
			List<CmsNavbar> cmsNavbar = cmsService.getCmsNavbar(queryParams,0,count);
			for (int i = 0; i < cmsNavbar.size(); i++) {
				resultMap = new HashMap<String, Object>();
				resultMap.put("id", cmsNavbar.get(i).getId());
				resultMap.put("name", cmsNavbar.get(i).getNameZh());
				resultMap.put("pId", cmsNavbar.get(i).getParentId());
				resultList.add(resultMap);
			}
			result.put("msg", "OK");
			result.put("data", resultList);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * toCmsNavbarAddUpdate:添加导航条 <br/> 
	 * 
	 * @author xwh
	 * @param nameZh	中文名称
	 * @param nameEn	英文名称 
	 * @param iconImg	图标
	 * @param showLocation	显示位置 
	 * @param mineParentId	父节点
	 * @param sorts	排序 
	 * @param status	状态
	 * @param id	当前id
	 * @param types	类型
	 * @param relevanceUrl	外部链接URL
	 * @param relevanceClasses	关联知识库
	 * @param categoryId	关联新闻栏目
	 * @param relevancePageUrl	关联站内链接URL
	 * @param permissionsId	权限用户
	 * @param openWith	页面打开方式
	 * @return 
	 *
	 */
	@RequestMapping("cmsNavbarAddUpdate")
	@ResponseBody
	public Map<String,Object> toCmsNavbarAddUpdate(String nameZh,String nameEn,String iconImg,String showLocation,String mineParentId,String sorts,String status,
			String id,String types,String relevanceUrl,String relevanceClasses,String categoryId,String relevancePageUrl,String permissionsId,String openWith){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			CmsNavbar cmsNavbar = new CmsNavbar();
			cmsNavbar.setIconImg(iconImg);
			cmsNavbar.setNameEn(nameEn);
			cmsNavbar.setNameZh(nameZh);
			cmsNavbar.setShowLocation(Integer.parseInt(showLocation));
			cmsNavbar.setParentId(Integer.parseInt(mineParentId));
			cmsNavbar.setSort(Integer.parseInt(sorts));
			cmsNavbar.setStatus(Integer.parseInt(status));
			cmsNavbar.setType(Integer.parseInt(types));
			if(types.equals("3")){
				cmsNavbar.setRelevanceUrl(relevanceUrl);
			}
			if(types.equals("1") || types.equals("2")){
				cmsNavbar.setRelevanceClassesEn(relevanceClasses);
			}
			if(types.equals("0")){
				cmsNavbar.setRelevanceCategoryId(categoryId);
			}
			if(types.equals("4")){
				cmsNavbar.setRelevancePageUrl(relevancePageUrl);
			}
			cmsNavbar.setPermissions(permissionsId);
			cmsNavbar.setOpenWith(Integer.parseInt(openWith));
			if(id.equals("0")){
				cmsService.saveCmsNavbar(cmsNavbar);
			}else{
				cmsNavbar.setId(Integer.parseInt(id));
				cmsService.editCmsNavbar(cmsNavbar);
			}
			result.put("msg", "OK");
			result.put("data", "");
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * toQueryCmsNavbar: 显示全部的导航条<br/> 
	 * 
	 * @author xwh
	 * @param nameZh	中文名称
	 * @param nameEn	英文名称
	 * @param page		第几页
	 * @return 
	 *
	 */
	@RequestMapping("queryCmsNavbar")
	@ResponseBody
	public Map<String,Object> toQueryCmsNavbar(String nameZh,String nameEn,String page){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			if(nameZh != null && !"".equals(nameZh)){
				queryParams.put("%,name_zh", nameZh);
			}
			if(nameEn != null && !"".equals(nameEn)){
				queryParams.put("%,name_en", nameEn);
			}
			queryParams.put("^,sort", "asc");
			Integer count = cmsService.getCmsNavbarCount(queryParams);
			List<CmsNavbar> cmsNavbar = cmsService.getCmsNavbar(queryParams, Integer.parseInt(page), 15);
			Map<String,Object> resultMap = null;
			List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
			for (int i = 0; i < cmsNavbar.size(); i++) {
				resultMap = new HashMap<String, Object>();
				resultMap.put("nameZh", cmsNavbar.get(i).getNameZh());
				resultMap.put("id", cmsNavbar.get(i).getId());
				resultMap.put("nameEn", cmsNavbar.get(i).getNameEn());
				if(cmsNavbar.get(i).getShowLocation() == 0){
					resultMap.put("showLocation", "头部");
				}else if(cmsNavbar.get(i).getShowLocation() == 1){
					resultMap.put("showLocation", "底部");
				}else if(cmsNavbar.get(i).getShowLocation() == 2){
					resultMap.put("showLocation", "右侧");
				}else if(cmsNavbar.get(i).getShowLocation() == 3){
					resultMap.put("showLocation", "都显示");
				}
				
				if(cmsNavbar.get(i).getType() == 0){
					resultMap.put("type", "关联新闻栏目");
				}else if(cmsNavbar.get(i).getType() == 1){
					resultMap.put("type", "关联知识库");
				}else if(cmsNavbar.get(i).getType() == 2){
					resultMap.put("type", "关联知识导航页");
				}else if(cmsNavbar.get(i).getType() == 3){
					resultMap.put("type", "外部链接");
				}else if(cmsNavbar.get(i).getType() == 4){
					resultMap.put("type", "关联站内链接");
				}
				if(cmsNavbar.get(i).getStatus() == 0){
					resultMap.put("status", "启用");
				}else if(cmsNavbar.get(i).getStatus() == 1){
					resultMap.put("status", "禁用");
				}
				resultList.add(resultMap);
			}
			result.put("msg", "OK");
			result.put("data", resultList);
			result.put("count", count);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * toDeleteCmsNavbar: 导航条删除<br/> 
	 * 
	 * @author xwh
	 * @param id		导航条ID
	 * @return 
	 *
	 */
	@RequestMapping("deleteCmsNavbar")
	@ResponseBody
	public Map<String,Object> toDeleteCmsNavbar(String id){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			CmsNavbar cmsNavbar = cmsService.findCmsNavbar(Integer.parseInt(id));
			cmsService.deleteCmsNavbar(cmsNavbar);
			result.put("msg", "OK");
			result.put("data", "");
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * toFindCmsNavbarById:根据ID去查询导航条 <br/> 
	 * 
	 * @author xwh
	 * @param id	当前id
	 * @return 
	 *
	 */
	@RequestMapping("findCmsNavbarById")
	@ResponseBody
	public Map<String,Object> toFindCmsNavbarById(String id){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			CmsNavbar cmsNavbar = cmsService.findCmsNavbar(Integer.parseInt(id));
			Map<String,Object> tree = new HashMap<String, Object>();
			if(cmsNavbar.getParentId() == 0){
				tree.put("parentId", 0);
				tree.put("parentName", "根节点");
			}else{
				tree.put("parentId", cmsNavbar.getParentId());
				tree.put("parentName", cmsService.findCmsNavbar(cmsNavbar.getParentId()).getNameZh());
			}
			if(cmsNavbar.getType() == 0){
//				System.out.println(cmsNavbar.getRelevanceCategoryId());
				if(cmsNavbar.getRelevanceCategoryId().equals("0")){
					tree.put("categoryId", 0);
					tree.put("categoryName", "根节点");
				}else{
					HashMap<String, Object> queryParams = new HashMap<String, Object>();
					queryParams.put("=,value", cmsNavbar.getRelevanceCategoryId());
					tree.put("categoryId", cmsNavbar.getRelevanceCategoryId());
					tree.put("categoryName", publicService.getDictItem(queryParams, 0, 2).get(0).getNameZh());
				}
			}
			if(!cmsNavbar.getPermissions().equals("*")){
				HashMap<String, Object> queryParams = new HashMap<String, Object>();
				queryParams.put("=,username", cmsNavbar.getPermissions());
				tree.put("permissionsId", cmsNavbar.getPermissions());
				tree.put("permissionsName", publicService.getMember(queryParams, 0, 2).get(0).getUsername());
			}else{
				tree.put("permissionsId", 0);
				tree.put("permissionsName", "*");
			}
			result.put("msg", "OK");
			result.put("data", cmsNavbar);
			result.put("tree", tree);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}

	
	/**
	 * 
	 * toaddUpdateCmsNewsMoudle: 添加修改内容块的方法<br/> 
	 * 
	 * @author xwh
	 * @param nameZh	中文名称
	 * @param nameEn	英文名称
	 * @param sort		排序
	 * @param status	状态
	 * @param showMore	是否显示更多
	 * @param layoutStyle	布局样式
	 * @param dataType	数据类型
	 * @param moduleType	块类型
	 * @param recommend		是否推荐数据
	 * @param permissionsId	权限用户
	 * @param openWith		页面打开方式
	 * @param datasetId		关联id
	 * @param datasetText	数据集
	 * @param htmlText		html内容
	 * @param id
	 * @return 
	 *
	 */
	@RequestMapping("addUpdateCmsNewsMoudle")
	@ResponseBody
	public Map<String,Object> toaddUpdateCmsNewsMoudle(String nameZh, String nameEn, String sort, String status, String showMore, String layoutStyle, String dataType,
			String moduleType, String recommend, String permissionsId, String openWith, String datasetId, String datasetText, String htmlText, String id,
			String styleCss,String contentType,String relevanceClasses){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			CmsNewsModule cmsNewsModule = new CmsNewsModule();
			cmsNewsModule.setDataType(Integer.parseInt(dataType));
			cmsNewsModule.setNameZh(nameZh);
			cmsNewsModule.setNameEn(nameEn);
			cmsNewsModule.setSort(Integer.parseInt(sort));
			cmsNewsModule.setStatus(Integer.parseInt(status));
			cmsNewsModule.setShowMore(Integer.parseInt(showMore));
			cmsNewsModule.setLayoutStyle(Integer.parseInt(layoutStyle));
			cmsNewsModule.setModuleType(Integer.parseInt(moduleType));
			cmsNewsModule.setRecommend(Integer.parseInt(recommend));
			cmsNewsModule.setPermissions(permissionsId);
			cmsNewsModule.setOpenWith(Integer.parseInt(openWith));
			if(Integer.parseInt(contentType) == 0){
				styleCss = "newlist"+ styleCss;
			}
			if(Integer.parseInt(contentType) == 1){
				styleCss = "image-text-"+ styleCss;
			}
			cmsNewsModule.setCssStyle(styleCss);
			cmsNewsModule.setContentType(Integer.parseInt(contentType));
			if(dataType.equals("0")){
				if(Integer.parseInt(moduleType) == 0){
					cmsNewsModule.setDataset(datasetText);
				}
				if(Integer.parseInt(moduleType) == 1){
					cmsNewsModule.setDataset(datasetText);
				}
				if(Integer.parseInt(moduleType) == 2){
					cmsNewsModule.setHtmlText(htmlText);
				}
			}
			if(dataType.equals("1")){
				if(Integer.parseInt(moduleType) == 0){
					cmsNewsModule.setRelevanceCategoryId(datasetId);
				}
				if(Integer.parseInt(moduleType) == 1){
					cmsNewsModule.setRelevanceClassesEn(relevanceClasses);
				}
			}
			if(id.equals("0")){
				cmsService.saveCmsNewsModule(cmsNewsModule);
			}else{
				cmsNewsModule.setId(Integer.parseInt(id));
				cmsService.editCmsNewsModule(cmsNewsModule);
			}
			result.put("msg", "OK");
			result.put("data", "");
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * toClassTypeShow: 内容块为关联数据集资源库显示<br/> 
	 * 
	 * @author xwh
	 * @param classesEn	对应资源库的英文名称
	 * @param text	查询对应的词
	 * @param page	对应页码
	 * @param treeId	对应的栏目ID
	 * @return 
	 *
	 */
	@RequestMapping("classTypeShow")
	@ResponseBody
	public Map<String,Object> toClassTypeShow(String classesEn,String text,String page,String treeId){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			HashMap<String,Object> queryParamc = new HashMap<String, Object>();
			queryParamc.put("=,name_en", classesEn);
			List<Classes> classes = publicService.getClasses(queryParamc,0,2);
			HashMap<String,Object> queryParams = new HashMap<String, Object>();
			queryParams.put("=,classes_id", classes.get(0).getId());
			Integer dataPropertyCount = publicService.getDataPropertyCount(queryParams);
			List<DataProperty> dataProperty = publicService.getDataProperty(queryParams,0,dataPropertyCount);
			String yesOrNo = "no";
			for (int i = 0; i < dataProperty.size(); i++) {
				if(dataProperty.get(i).getNameEn().equals("cover")){
					yesOrNo = "yes";
					break;
				}
			}
			Map<String,Object> conditions = new HashMap<String, Object>();
			if(!"".equals(text) && text != null){
				conditions.put("title", text);
			}
			conditions.put("=status", 1);
			if(!"0".equals(treeId)){
				conditions.put("=news_category", publicService.findDictItem(Integer.parseInt(treeId)).getValue());
			}
			Long count = dynamicService.getDynamicCount(classesEn, conditions);
			List<Map<String, Object>> dynamicData = dynamicService.getDynamicData(classesEn, conditions, Integer.parseInt(page), 10);
			result.put("msg", "OK");
			result.put("data", dynamicData);
			result.put("count", count);
			result.put("yesOrNo", yesOrNo);
			result.put("imagePath", XxlConfClient.get("dal.file.server", "") +"/");
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}
	
	
	/**
	 * 
	 * toCmsNewsShowAll:首页显示带查询 <br/> 
	 * 
	 * @author xwh
	 * @param page	对应页数
	 * @param nameZh	中文名称
	 * @param nameEn	英文名称
	 * @return 
	 *
	 */
	@RequestMapping("cmsNewsShowAll")
	@ResponseBody
	public Map<String,Object> toCmsNewsShowAll(String page,String nameZh,String nameEn){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			if(nameZh != null && !"".equals(nameZh)){
				queryParams.put("%,name_zh", nameZh);
			}
			if(nameEn != null && !"".equals(nameEn)){
				queryParams.put("%,name_en", nameEn);
			}
			queryParams.put("^,sort", "asc");
			Integer count = cmsService.getCmsNewsModuleCount(queryParams);
			List<CmsNewsModule> cmsNewsModule = cmsService.getCmsNewsModule(queryParams, Integer.parseInt(page), 15);
			Map<String,Object> resultMap = null;
			List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
			for (int i = 0; i < cmsNewsModule.size(); i++) {
				resultMap = new HashMap<String, Object>();
				resultMap.put("nameZh", cmsNewsModule.get(i).getNameZh());
				resultMap.put("id", cmsNewsModule.get(i).getId());
				resultMap.put("nameEn", cmsNewsModule.get(i).getNameEn());
				if(cmsNewsModule.get(i).getDataType()==0){
					resultMap.put("dataType", "固定数据");
				}
				if(cmsNewsModule.get(i).getDataType()==1){
					resultMap.put("dataType", "动态数据");
				}
				/*if(cmsNewsModule.get(i).getLayoutStyle()==0){
					resultMap.put("layoutStyle", "栅格12");
				}
				if(cmsNewsModule.get(i).getLayoutStyle()==1){
					resultMap.put("layoutStyle", "栅格6x6");
				}
				if(cmsNewsModule.get(i).getLayoutStyle()==2){
					resultMap.put("layoutStyle", cmsNewsModule.get(i).getLayoutStyle());
				}*/
				resultMap.put("layoutStyle", cmsNewsModule.get(i).getLayoutStyle());
				if(cmsNewsModule.get(i).getStatus()==0){
					resultMap.put("status", "启用");
				}
				if(cmsNewsModule.get(i).getStatus()==1){
					resultMap.put("status", "禁用");
				}
				resultList.add(resultMap);
			}
			result.put("msg", "OK");
			result.put("data", resultList);
			result.put("count", count);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * toDeleteNewsModule:删除对应的内容块 <br/> 
	 * 
	 * @author xwh
	 * @param id
	 * @return 
	 *
	 */
	@RequestMapping("deleteCmsNewsModule")
	@ResponseBody
	public Map<String,Object> toDeleteNewsModule(String id){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			CmsNewsModule cmsNewsModule = cmsService.findCmsNewsModule(Integer.parseInt(id));
			cmsService.deleteCmsNewsModule(cmsNewsModule);
			result.put("msg", "OK");
			result.put("data", "");
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * toFindNewsModule:返回修改根据id查询 <br/> 
	 * 
	 * @author xwh
	 * @param id	对应id
	 * @return 
	 *
	 */
	@RequestMapping("findCmsNewsModule")
	@ResponseBody
	public Map<String,Object> toFindNewsModule(String id){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			CmsNewsModule cmsNewsModule = cmsService.findCmsNewsModule(Integer.parseInt(id));
			Map<String,Object> tree = new HashMap<String, Object>();
			if(!cmsNewsModule.getPermissions().equals("*")){
				HashMap<String, Object> queryParams = new HashMap<String, Object>();
				queryParams.put("=,username", cmsNewsModule.getPermissions());
				tree.put("permissionsId", cmsNewsModule.getPermissions());
				tree.put("permissionsName", publicService.getMember(queryParams, 0, 2).get(0).getUsername());
			}else{
				tree.put("permissionsId", 0);
				tree.put("permissionsName", "*");
			}
			if(cmsNewsModule.getDataType() == 1){
				if(cmsNewsModule.getModuleType() == 0){
					HashMap<String, Object> queryParams = new HashMap<String, Object>();
					queryParams.put("=,value", cmsNewsModule.getRelevanceCategoryId());
//					publicService.getDictItem(queryParams, 0, 2);
					tree.put("CategoryId", cmsNewsModule.getRelevanceCategoryId());
					tree.put("CategoryName", publicService.getDictItem(queryParams, 0, 2).get(0).getNameZh());
				}
			}
			cmsNewsModule.setCssStyle(cmsNewsModule.getCssStyle().substring(cmsNewsModule.getCssStyle().length()-1, cmsNewsModule.getCssStyle().length()));
			result.put("msg", "OK");
			result.put("data", cmsNewsModule);
			result.put("tree", tree);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		return result;
	}
	
	

	// 门户内容管理 -->分类管理树形菜单列表-->查询菜单
		@RequestMapping("cmsCategoryDictItem")
		public void cmsCategoryDictItem(HttpServletResponse response)throws Exception {
			List<HashMap<String, Object>> r = new ArrayList<HashMap<String, Object>>();
			LinkedHashMap<String, Object> mp = null;
			List<DictItem> dictItemList = publicService.getDictItem(publicService.findDict(10));
			for (int i = 0; i < dictItemList.size(); i++) {
				DictItem dictItem = dictItemList.get(i);
				mp = new LinkedHashMap<String, Object>();
				mp.put("id", dictItem.getId());
				mp.put("name", dictItem.getNameZh()); // 及 nameZh 中文名称
				mp.put("nameEn", dictItem.getNameEn());
				mp.put("value", dictItem.getValue());
				mp.put("sort", dictItem.getSort());
				mp.put("dictId", dictItem.getDictId());
				if (dictItem.getDictItemId() != null && dictItem.getDictItemId() != 0) {
					DictItem d = publicService.findDictItem(dictItem.getDictItemId());
					mp.put("pId", d.getId());
				} else {
					mp.put("pId", "0");
				}
				r.add(mp);
			}
			String result = formatToJson(r);
			super.pringWriterToPage(result.toString(), "application/json", response);
		}		
}
